﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using sqlserverextensions.Helper;


    public partial class UserDefinedFunctions
    {
        [Microsoft.SqlServer.Server.SqlFunction(
           DataAccess = DataAccessKind.None,
           FillRowMethodName = "FillSplitRowList",
           IsDeterministic = true,
           Name = "Split",
           TableDefinition = @"   Value NVARCHAR(4000)"
           )]
        public static IEnumerable Split(SqlString chars, SqlString splittingChar)
        {
            Int16 errSeverity = 16;

            try
            {

                return chars.Value.Split(new string[] { splittingChar.Value }, StringSplitOptions.None);
            }
            catch (Exception ex)
            {
                ExceptionHelper.EvaluateException(ex, errSeverity);
                return null;
            }
        }

        private static void FillSplitRowList(object obj, out SqlString value)
        {
            try
            {
                if (obj is String)
                {
                    value = obj.ToString();
                }
                else
                {
                    value = null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

